
package com.ljt.controller;

import com.ljt.admin.dto.DeptTree;
import com.ljt.admin.entity.SysDept;
import com.ljt.common.utils.BaseResult;
import com.ljt.common.utils.BaseResultGenerator;
import com.ljt.common.utils.annotation.SysLog;
import com.ljt.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author administrator
 * @date 2019/07/25
 * @since 2019/2/1
 */
@Api(value = "部门管理", tags = {"部门管理"})
@RestController
@AllArgsConstructor
@RequestMapping("/dept")
public class DeptController {
	private final SysDeptService sysDeptService;

	/**
	 * 通过ID查询
	 *
	 * @param id ID
	 * @return SysDept
	 */
	@ApiOperation(value = "通过ID查询", notes = "作者：administrator")
	@ApiImplicitParam(paramType="path", name = "id", value = "对象id", required = true, dataType = "Integer")
	@GetMapping("/{id}")
	public BaseResult<SysDept> getById(@PathVariable Integer id) {
		return BaseResultGenerator.success(sysDeptService.getById(id));
	}


	/**
	 * 返回树形菜单集合
	 *
	 * @return 树形菜单
	 */
	@ApiOperation(value = "树形菜单集合", notes = "作者：administrator")
	@GetMapping(value = "/tree")
	public BaseResult<List<DeptTree>> listDeptTrees() {
		return BaseResultGenerator.success(sysDeptService.listDeptTrees());
	}

	/**
	 * 返回当前用户树形菜单集合
	 *
	 * @return 树形菜单
	 */
	@ApiOperation(value = "当前用户树形菜单集合", notes = "作者：administrator")
	@GetMapping(value = "/user-tree")
	public BaseResult<List<DeptTree>> listCurrentUserDeptTrees() {
		return BaseResultGenerator.success(sysDeptService.listCurrentUserDeptTrees());
	}

	/**
	 * 添加
	 *
	 * @param sysDept 实体
	 * @return success/false
	 */
	@ApiOperation(value = "添加部门", notes = "作者：administrator")
	@SysLog("添加部门")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_add')")
	public BaseResult<Boolean> save(@Valid @RequestBody SysDept sysDept) {
		return BaseResultGenerator.success(sysDeptService.saveDept(sysDept));
	}

	/**
	 * 删除
	 *
	 * @param id ID
	 * @return success/false
	 */
	@ApiOperation(value = "删除部门", notes = "作者：administrator")
	@ApiImplicitParam(paramType="path", name = "id", value = "对象id", required = true, dataType = "Integer")
	@SysLog("删除部门")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dept_del')")
	public BaseResult<Boolean> removeById(@PathVariable Integer id) {
		return BaseResultGenerator.success(sysDeptService.removeDeptById(id));
	}

	/**
	 * 编辑
	 *
	 * @param sysDept 实体
	 * @return success/false
	 */
	@ApiOperation(value = "编辑部门", notes = "作者：administrator")
	@SysLog("编辑部门")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_edit')")
	public BaseResult<Boolean> update(@Valid @RequestBody SysDept sysDept) {
		sysDept.setUpdateTime(LocalDateTime.now());
		return BaseResultGenerator.success(sysDeptService.updateDeptById(sysDept));
	}
}
